/*
 * Powered By [rapid-framework]
 * Csdn Code: generate simple CRUD code in single table.
 * Since 2015 - 2017
 */

package com.vcredit.creditcat.service;

import com.vcredit.creditcat.model.ProductCondition;
import com.vcredit.creditcat.persistence.base.ProductConditionMapper;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

/*
 * @author zhangxingong
 * @version 1.0
 * @since 1.0
*/
@Service
public class ProductConditionService {

  @Autowired
  private ProductConditionMapper productConditionMapper;

  public List<ProductCondition> findByProductId(Long productId) {

    List<ProductCondition> conditions = this.findAll();
    Iterator<ProductCondition> iterator = conditions.iterator();
    while (iterator.hasNext()) {
      ProductCondition condition = iterator.next();
      if (ObjectUtils.notEqual(productId, condition.getProductId())) {
        iterator.remove();
      }
    }
    return conditions;
  }

  @Cacheable(value = "productCondition", key = "'condition:productCondition_'+#productId+ '_' + #type")
  public List<ProductCondition> findByProductIdAndType(Long productId, String type) {

    List<ProductCondition> conditions = this.findAll();
    Iterator<ProductCondition> iterator = conditions.iterator();
    while (iterator.hasNext()) {
      ProductCondition condition = iterator.next();
      if (ObjectUtils.notEqual(productId, condition.getProductId()) || !StringUtils.equals(type, condition.getConditionType())) {
        iterator.remove();
      }
    }
    return conditions;
  }

  @Cacheable(value = "productCondition", key = "'condition:productCondition_'+#productId")
  public List<ProductCondition> findByProductIdAndSelected(Long productId) {

    List<ProductCondition> conditions = this.findAll();
    Iterator<ProductCondition> iterator = conditions.iterator();
    while (iterator.hasNext()) {
      ProductCondition condition = iterator.next();
      int i = Integer.parseInt(condition.getConditionType());
      if (ObjectUtils.notEqual(productId, condition.getProductId()) || i > 2) {
        iterator.remove();
      }
    }
    return conditions;
  }

  public List<ProductCondition> find(ProductCondition productCondition) {
    return productConditionMapper.find(productCondition);
  }

  public ProductCondition findById(Long productConditionId) {
    return productConditionMapper.findById(productConditionId);
  }

  @Cacheable(value = "productCondition",key = "'condition:productConditions'")
  public List<ProductCondition> findAll() {
    return productConditionMapper.findAll();
  }

  @CacheEvict(value = "productCondition", key = "'condition:productCondition*'")
  public void insert(ProductCondition productCondition) {
    productConditionMapper.insert(productCondition);
  }

  @CacheEvict(value = "productCondition", key = "'productCondition*'")
  public void update(ProductCondition productCondition) {
    productConditionMapper.update(productCondition);
  }

  @CacheEvict(value = "productCondition", key = "'productCondition*'", beforeInvocation = true)
  public void clearProductConditionCache() {
    System.out.println("clearProductConditionCache");
  }
}
